package com.fcy.teamwork.business.projectlife

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.fcy.teamwork.App
import com.fcy.teamwork.R
import com.fcy.teamwork.base.BaseActivity
import com.fcy.teamwork.base.fromJson
import com.fcy.teamwork.base.toJson
import com.fcy.teamwork.business.newjob.NewJobActivity
import com.fcy.teamwork.business.self.SelfInfoAct
import com.fcy.teamwork.databinding.ActControllProjectBinding
import com.fcy.teamwork.databinding.LlMemberInfoBinding
import kotlinx.coroutines.launch

class ProjectControllerAct : BaseActivity<ActControllProjectBinding>() {
    private val vm: ProjectControllerVM by viewModels()

    private val adapter by lazy { ProjectsPlanAdapter(vm) }
    override fun initBinding(): ActControllProjectBinding =
        ActControllProjectBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        parseArgs()
        binding.actionBar.ibBack.setOnClickListener {
            finish()
        }
        binding.rvProjectPlan.also {
            it.adapter = adapter
            it.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        }
        adapter.initIndicator(
            binding.tvSelectInfo,
            binding.fabFinish,
            binding.fabCancelFinish,
            binding.fabNewPlanNode
        )
        binding.actionBar.apply {
            if (vm.projectInfo.ownerId == App.person.personId) {
                ibFinish.visibility = View.VISIBLE
                ibFinish.setImageResource(R.drawable.ic_baseline_add_24)
                ibFinish.setOnClickListener {
                    Intent(
                        this@ProjectControllerAct,
                        NewJobActivity::class.java
                    ).also { intent ->
                        intent.putExtra("project", vm.projectInfo.toJson())
                        startActivity(intent)
                    }
                }
            }
            tvTitle.text = vm.projectInfo.name
        }
        lifecycleScope.launch {
            vm.loadAllNodes()
            vm.allNodesFlow.collect { nodes ->
                adapter.submitData(nodes)
            }
        }
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED) {
                binding.fvMembers.removeAllViews()
                vm.loadAllMember().forEach { person ->
                    LlMemberInfoBinding.inflate(layoutInflater, binding.fvMembers, true)
                        .apply {
                            portrait.setImageResource(R.drawable.por1)
                            portrait.setOnClickListener {
                                Intent(this@ProjectControllerAct, SelfInfoAct::class.java).apply {
                                    putExtra("canSenMsg", true)
                                    putExtra("userName", person.personId)
                                    putExtra("userInfo", person.toJson())
                                    root.context.startActivity(this)
                                }
                            }
                            tvName.text = person.nikeName
                            portrait.setOnLongClickListener {
                                DeleteFromGroupDialog().apply {
                                    listsner = { boo: Boolean ->
                                        if (boo)
                                            all.visibility = View.GONE
                                    }
                                    arguments = Bundle().apply {
                                        putString("personId", person.personId)
                                        putString("projectId", vm.projectInfo.groupId)
                                    }
                                    show(supportFragmentManager, "tag")
                                }
                                true
                            }
                        }
                }
            }
        }
    }

    private fun parseArgs() {
        vm.projectInfo =
            intent.getStringExtra("projectInfo")?.fromJson()
                ?: throw IllegalArgumentException("未传入指定的projectInfo")
    }

}